//
//  ZhuCeViewController.m
//  小小医
//
//  Created by ym on 16/3/31.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "ZhuCeViewController.h"
#import "AddressPickView.h"
#import "Z_PopView.h"
#import "CaptchaView.h"
#import "NetWorkTools.h"
@interface ZhuCeViewController ()<UIScrollViewDelegate,UITextFieldDelegate, UIAlertViewDelegate>
{
    CaptchaView *authCodeView;
    UITextField *_input;
    NSString *BankInfo;
    NSMutableArray *addressArr;
    
}
@property (strong, nonatomic) Z_PopView *popView;
@property (weak, nonatomic) IBOutlet UIView *BackViewForAll;

@property (weak, nonatomic) IBOutlet UIScrollView *bakGround;

//验证码
@property (weak, nonatomic) IBOutlet UIView *BackView;

@property (weak, nonatomic) IBOutlet UIButton *adderssBtn;


@property (weak, nonatomic) IBOutlet UITextField *XiangXiAddress;
@property (weak, nonatomic) IBOutlet UITextField *Name;

@property (weak, nonatomic) IBOutlet UITextField *Phone;

//银行信息
// 卡号
@property (weak, nonatomic) IBOutlet UITextField *BankNUm;
// 开户行
@property (weak, nonatomic) IBOutlet UITextField *BankInfo;
// 选择银行
@property (weak, nonatomic) IBOutlet UIButton *ChoseBankBtn;

// 验证码
@property (weak, nonatomic) IBOutlet UITextField *YanZhengMa;
@property (nonatomic, assign) BOOL isSelected;
@end
@implementation ZhuCeViewController





- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadUserAddressData];
    [self Bankdata];
    self.XiangXiAddress.delegate = self;
    self.Name.delegate = self;
    self.Phone.delegate = self;
    self.BankNUm.delegate = self;
    self.BankInfo.delegate = self;
//    self.YanZhengMa.delegate = self;
    // Do any additional setup after loading the view from its nib.
    self.title = @"完善用户信息";
     self.navigationController.navigationBar.translucent = NO;
    
    authCodeView = [[CaptchaView alloc] initWithFrame:CGRectMake(0, 0, self.BackView.bounds.size.width, self.BackView.bounds.size.height)];
    [self.BackView addSubview:authCodeView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(registerLogInTextField)];
    // 点击次数
    tap.numberOfTapsRequired = 1;
    // 手指个数
    tap.numberOfTouchesRequired = 1;
    // 给视图 hn添加手势
    [self.BackViewForAll addGestureRecognizer:tap];

}



- (IBAction)Address:(id)sender {
    AddressPickView *addressPickView = [AddressPickView shareInstance];
    [self.view addSubview:addressPickView];
    addressPickView.block = ^(NSString *province,NSString *city,NSString *town){
//        NSLog(@"%@%@%@",province,city,town);
        [self.adderssBtn setTitle:[NSString stringWithFormat:@"%@%@%@",province,city,town] forState:UIControlStateNormal];
        addressArr = [NSMutableArray arrayWithObjects:province,city,town, nil];

        
    };
}



- (IBAction)Bank:(id)sender {
    //  self.dataArr  = [NSMutableArray arrayWithObjects:@"中国人民银行",@"国家开发银行",@"中国进出口银行", @"中国农业发展银行", @"中国工商银行", @"中国建设银行", @"中国银行", @"中国农业银行", @"交通银行", @"招商银行", @"中国邮政储蓄银行", @"中信银行", @"光大银行", @"民生银行", @"兴业银行", @"华夏银行", @"上海银行", @"平安银行", @"浙商银行",@"杭州银行",nil];
    
    self.bakGround.scrollEnabled = NO ;
    _isSelected = !_isSelected;
    _popView = [[Z_PopView alloc] initWithArray:@[@"中国人民银行",@"国家开发银行",@"中国进出口银行", @"中国农业发展银行", @"中国工商银行", @"中国建设银行", @"中国银行", @"中国农业银行", @"交通银行", @"招商银行", @"中国邮政储蓄银行", @"中信银行", @"光大银行", @"民生银行", @"兴业银行", @"华夏银行", @"上海银行", @"平安银行", @"浙商银行",@"杭州银行"]];
    _popView.chooseBlock = ^(NSString *chooseItem) {
        _isSelected = NO;
        [sender setTitle:chooseItem forState:UIControlStateNormal];
        BankInfo = chooseItem;
        self.bakGround.scrollEnabled = YES ;
        // 回调 点击
    };
    
    if (_isSelected) {
        //         self.backGround.scrollEnabled = NO ;
        // 未被点击
//        _popView.i = self.i;
        [_popView showInView:self.bakGround baseView:sender withPosition:ZShowBottom];
        self.bakGround.scrollEnabled = NO ;
    } else {
        [UIView animateWithDuration:.3 animations:^{
            [self.popView setAlpha:0];
            self.bakGround.scrollEnabled = YES ;
        } completion:^(BOOL finished) {
            
            [self.popView removeFromSuperview];
            self.bakGround.scrollEnabled = YES ;
        }];
    }

    
}

- (IBAction)FinishBtn:(id)sender {
   
    
    if ([self.YanZhengMa.text isEqualToString:authCodeView.authCodeStr]) {
        if (BankInfo.length== 0 || self.BankNUm.text.length==0 || self.BankInfo.text.length == 0 ||  self.XiangXiAddress.text.length == 0 || self.Name.text.length == 0 || self.Phone.text.length == 0 ) {
            
            UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完整信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alview show];
        }else{
            [self data];
        }
       
    }else{
        
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请重新填写验证码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        self.YanZhengMa.text = @"";
        [alview show];
    }
    
    
    
}

- (void)data{
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    NSString *userNumber = [dict objectForKey:@"Number"];
    NSDictionary *params = @{
                             @"U_Number":userNumber,
                             @"Bank_Info":BankInfo,
                             @"Bank_Code":self.BankNUm.text,
                             @"WW":self.BankInfo.text,  // 开户行
                             @"Province":addressArr[0],
                             @"City":addressArr[1],
                             @"Countory":addressArr[2],
                             @"Address":self.XiangXiAddress.text,
                             @"ContactName":self.Name.text,
                             @"ContactPhone":self.Phone.text,
                             
                             @"IsDefault":@"1"
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"PERFECTIONUSERINFO" completeBlock:^(id object) {
//        NSLog(@"%@",object);
        
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"Message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alview show];
        
        
        
    }];

    
}
// 默认地址信息
- (void)loadUserAddressData {
    
    
    NSDictionary *userData = [ReadLoginData readLoginData];
    
    NSString *userId = [userData objectForKey:@"Number"];
    
    NSDictionary *params = @{
                             
                             @"UserId":userId
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"EXPRESSQUERY" completeBlock:^(id object) {
        NSString *string = [object objectForKey:@"BackData"];
        NSArray *arr = [NetWorkTools arrayWithJsonString:string];
        NSLog(@"arrarrarr%@",arr);
        if (arr.count) {
            [self.adderssBtn setTitle:[NSString stringWithFormat:@"%@%@%@",arr[0][@"Province"],arr[0][@"City"],arr[0][@"Countory"]] forState:UIControlStateNormal];
            
            addressArr = [NSMutableArray arrayWithObjects:arr[0][@"Province"],arr[0][@"City"],arr[0][@"Countory"], nil];
            self.Name.text = arr[0][@"ContactName"];
            self.Phone.text = [NSString stringWithFormat:@"%@",arr[0][@"ContactPhone"]];
            self.XiangXiAddress.text =[NSString stringWithFormat:@"%@",arr[0][@"Address"]];
            self.adderssBtn.userInteractionEnabled = NO;
            self.Name.userInteractionEnabled = NO;
            self.Phone.userInteractionEnabled = NO;
            self.XiangXiAddress.userInteractionEnabled = NO;
            NSString *str =[NSString stringWithFormat:@"%@",arr[0][@"Address"]];
            
            if (self.Name.text.length == 0 ) {
                self.Name.userInteractionEnabled = YES;
            }else if (self.Phone.text.length == 0){
                self.Phone.userInteractionEnabled = YES;
            }else if (self.XiangXiAddress.text.length == 0){
                self.XiangXiAddress.userInteractionEnabled = YES;
                
            }else if (str.length == 0){
                self.adderssBtn.userInteractionEnabled = YES;
            }
            
            
            
            
        }
       
    }];
}
- (void)Bankdata{
    NSDictionary *dict = [ReadLoginData readLoginData];
    
//       NSLog(@"%@",dict);
    
    NSString *userNumber = [dict objectForKey:@"Number"];
    NSDictionary *params = @{
                             
                             @"UserNumber":userNumber,
                             
                             };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"BANKINFOQUERY" completeBlock:^(id object) {
        NSString *string = [object objectForKey:@"BackData"];
        
       NSArray *arr = [NetWorkTools arrayWithJsonString:string];
        if (arr.count) {
            self.BankNUm.text = [NSString stringWithFormat:@"%@",arr[0][@"Bank_Code"]];
            // 开户行
            self.BankInfo.text = [NSString stringWithFormat:@"%@",arr[0][@"WW"]];
            
            [self.ChoseBankBtn setTitle: [NSString stringWithFormat:@"%@",arr[0][@"Bank_Info"]] forState:UIControlStateNormal];
            BankInfo =arr[0][@"Bank_Info"];
            self.BankInfo.userInteractionEnabled = NO;
            self.BankNUm.userInteractionEnabled = NO;
            self.ChoseBankBtn.userInteractionEnabled = NO;
            
            NSString *str = [NSString stringWithFormat:@"%@",arr[0][@"Bank_Info"]];
            // 如果字符串为空 则允许输入
            if (self.BankNUm.text.length == 0 ) {
                self.BankNUm.userInteractionEnabled = YES;
            }else if (self.BankInfo.text.length == 0){
                self.BankInfo.userInteractionEnabled = YES;
            }else if (str.length == 0){
                 self.ChoseBankBtn.userInteractionEnabled = YES;
                
            }
            
        }
        
    }];
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 10001|| textField.tag == 10002|| textField.tag == 10003|| textField.tag == 10004|| textField.tag == 10005) {
        CGRect frame = textField.frame;
        int offset = frame.origin.y + 100 - (self.view.frame.size.height - 216.0);//键盘高度216
        
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        if(offset > 0)
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        
        [UIView commitAnimations];
        
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    self.view.frame =CGRectMake(0, 64, width, height);
    //    NSLog(@"%@", textField.text);
}
- (void)registerLogInTextField {
    [self.YanZhengMa resignFirstResponder];
   [self.XiangXiAddress resignFirstResponder];
    [self.Name resignFirstResponder];
    [self.Phone resignFirstResponder];
    [self.BankNUm resignFirstResponder];
    [self.BankInfo resignFirstResponder];
    
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self registerLogInTextField];
}


@end
